package cn.yuanqiao.archive.storeRoom.service.impl;

import cn.yuanqiao.archive.appManager.domain.DataScopeBase;
import cn.yuanqiao.archive.storeRoom.domain.*;
import cn.yuanqiao.archive.storeRoom.mapper.*;
import cn.yuanqiao.archive.storeRoom.service.IInAndOutBoundService;
import cn.yuanqiao.common.exception.ServiceException;
import cn.yuanqiao.common.utils.StringUtils;
import cn.yuanqiao.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class InAndOutBoundServiceImpl implements IInAndOutBoundService {

    @Autowired
    private InAndOutBoundMapper inAndOutBoundMapper;

    @Autowired
    private OutInLogsMapper outInLogsMapper;

    @Autowired
    private BoxMapper boxMapper;

    @Autowired
    private StoreroomMapper storeroomMapper;

    @Autowired
    private DaBoxMapper daBoxMapper;

    @Override
    public boolean inBound(List<OutInLogs> boxList) {
      //入库
      String code=SecurityUtils.getTenantCode().toUpperCase();
      if(StringUtils.isEmpty(code)){
        //抛出异常
          throw new ServiceException("TenantCode is null");
      }

      try {
            for(OutInLogs outInLogs:boxList){
                //需要判读改那个表
                String sjtype=outInLogs.getSjType();
                //按照盒
                if("0".equals(sjtype)){
                    DaBox daBox=new DaBox();
                    daBox.setDaId(outInLogs.getDaId());
                    daBox.setSubTableName(outInLogs.getSubTableName());
                    daBox.setOutboundStatus(2);
                    daBoxMapper.updateDaBox(daBox);
                    Box box=new Box();
                    box.setOutBoundStatus(2);
                    box.setBoxId(outInLogs.getBoxId());
                    boxMapper.updateBox(box);
                    OutInLogs outInLogs1=new OutInLogs();
                    outInLogs1.setCreator(SecurityUtils.getUserId());
                    outInLogs1.setCrkgzry(SecurityUtils.getLoginUser().getUser().getNickName());
                    outInLogs1.setDaId(outInLogs.getDaId());
                    outInLogs1.setCrkqr(new Date());
                    outInLogs1.setDh(outInLogs.getDh());
                    outInLogs1.setTitle(outInLogs.getTitle());
                    outInLogs1.setSubTableName(outInLogs.getSubTableName());
                    outInLogs1.setType("入库");
                    outInLogs1.setCreateTime(new Date());
                    outInLogs1.setStorecode(outInLogs.getStorecode());
                    outInLogs1.setStoreName(outInLogs.getStoreName());
                    outInLogs1.setSjType("0");
                    outInLogs1.setBoxId(outInLogs.getBoxId());
                    outInLogsMapper.insertOutInLogs(outInLogs1);

                }else if("1".equals(sjtype)){
                    Box box=new Box();
                    box.setOutBoundStatus(2);
                    box.setBoxId(outInLogs.getBoxId());
                    boxMapper.updateBox(box);
                    OutInLogs outInLogs1=new OutInLogs();
                    outInLogs1.setCreator(SecurityUtils.getUserId());
                    outInLogs1.setCrkgzry(SecurityUtils.getLoginUser().getUser().getNickName());
                    outInLogs1.setDaId(outInLogs.getDaId());
                    outInLogs1.setCrkqr(new Date());
                    outInLogs1.setDh(outInLogs.getDh());
                    outInLogs1.setTitle(outInLogs.getTitle());
                    outInLogs1.setSubTableName(outInLogs.getSubTableName());
                    outInLogs1.setType("入库");
                    outInLogs1.setCreateTime(new Date());
                    outInLogs1.setStorecode(outInLogs.getStorecode());
                    outInLogs1.setStoreName(outInLogs.getStoreName());
                    outInLogs1.setSjType("1");
                    outInLogs1.setBoxId(outInLogs.getBoxId());
                    outInLogsMapper.insertOutInLogs(outInLogs1);
                }
            }

          return true;
      }catch (Exception ex){
          ex.printStackTrace();
          return false;
      }

    }

    @Override
    public boolean outBound(OutInLogs outInLogs) {
        //入库
        String code=SecurityUtils.getTenantCode().toUpperCase();
        if(StringUtils.isEmpty(code)){
            //抛出异常
            throw new ServiceException("TenantCode is null");
        }
        List<OutBound> boxList=outInLogs.getOutBoundList();
        try {
            for(OutBound outBound:boxList) {
                //按盒上架
                String sjType=outBound.getSjType();
                if("0".equals(sjType)){
                    //盒出库需要修改yqda_box里的状态以及 yqbox里的状态，此处需要boxId以及daId 需要判断type:0组件 1组卷
                    Box box=new Box();
                    box.setBoxId(outBound.getBoxId());
                    box.setOutBoundStatus(1);
                    boxMapper.updateBox(box);
                    DaBox daBox=new DaBox();
                    daBox.setBoxId(outBound.getBoxId());
                    daBox.setSubTableName(outBound.getSubTableName());
                    daBox.setDaId(outBound.getDaId());
                    daBox.setOutboundStatus(1);
                    daBoxMapper.updateDaBox(daBox);
                    Storeroom storeroom=storeroomMapper.selectStoreroomByStoreId(outInLogs.getStoreId(),code);
                    OutInLogs outInLogs1=new OutInLogs();
                    outInLogs1.setCkyy(outInLogs.getCkyy());
                    outInLogs1.setCreator(SecurityUtils.getUserId());
                    outInLogs1.setCrkgzry(SecurityUtils.getLoginUser().getUser().getNickName());
                    outInLogs1.setRemark(outInLogs.getRemark());
                    outInLogs1.setDaId(outBound.getDaId());
                    outInLogs1.setCrkqr(new Date());
                    outInLogs1.setDh(outBound.getDh());
                    outInLogs1.setTitle(outBound.getTitle());
                    outInLogs1.setSubTableName(outBound.getSubTableName());
                    outInLogs1.setType("出库");
                    outInLogs1.setCreateTime(new Date());
                    outInLogs1.setStorecode(storeroom.getStoreCode());
                    outInLogs1.setStoreName(storeroom.getStoreName());
                    outInLogs1.setSjType("0");
                    outInLogs1.setBoxId(outBound.getBoxId());
                    outInLogsMapper.insertOutInLogs(outInLogs1);
                }else if("1".equals(sjType)){
                     //按卷上架
                    Box box=new Box();
                    box.setBoxId(outBound.getBoxId());
                    box.setOutBoundStatus(1);
                    boxMapper.updateBox(box);
                    Storeroom storeroom=storeroomMapper.selectStoreroomByStoreId(outInLogs.getStoreId(),code);
                    OutInLogs outInLogs1=new OutInLogs();
                    outInLogs1.setCkyy(outInLogs.getCkyy());
                    outInLogs1.setCreator(SecurityUtils.getUserId());
                    outInLogs1.setCrkgzry(SecurityUtils.getLoginUser().getUser().getNickName());
                    outInLogs1.setRemark(outInLogs.getRemark());
                    outInLogs1.setDaId(outBound.getDaId());
                    outInLogs1.setCrkqr(new Date());
                    outInLogs1.setDh(outBound.getDh());
                    outInLogs1.setTitle(outBound.getTitle());
                    outInLogs1.setSubTableName(outBound.getSubTableName());
                    outInLogs1.setType("出库");
                    outInLogs1.setCreateTime(new Date());
                    outInLogs1.setStorecode(storeroom.getStoreCode());
                    outInLogs1.setStoreName(storeroom.getStoreName());
                    outInLogs1.setSjType("1");
                    outInLogs1.setBoxId(outBound.getBoxId());
                    outInLogsMapper.insertOutInLogs(outInLogs1);
                }
            }
            return true;
        }catch (Exception ex){
            ex.printStackTrace();
            return false;
        }
    }

    @Override
    public List<OutBound> outList(Box box) {
        //需要一个字段判断是按盒还是按卷，需要格子Id
        String code=SecurityUtils.getTenantCode().toUpperCase();
        if(StringUtils.isEmpty(code)){
            //抛出异常
            throw new ServiceException("TenantCode is null");
        }
        String sjType=box.getSjType();
        List<OutBound> list=new ArrayList<>();
        if("0".equals(sjType)){
          list=inAndOutBoundMapper.getOutBoundByHe(code,box.getGridId(),box.getKeyword());
        }else if("1".equals(sjType)){
          list=inAndOutBoundMapper.getOutBoundByFolder(code,box.getGridId(),box.getKeyword());

        }
        return list;
    }

    @Override
    public List<OutInLogs> getInList(OutInLogs outInLogs) {
        String code=SecurityUtils.getTenantCode().toUpperCase();
        if(StringUtils.isEmpty(code)){
            //抛出异常
            throw new ServiceException("TenantCode is null");
        }
        return outInLogsMapper.getInList(new DataScopeBase(),outInLogs);
    }
}
